package com.wx.activitys.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.activitys.base.utils.WebUtil;
import com.wx.activitys.drp.JsonResult;
import com.wx.activitys.drp.EnumUtils.GradeState;
import com.wx.activitys.drp.model.BGrade;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.service.BGradeService;

@Controller
@RequestMapping("/grade")
public class GradeController {
	@Autowired
	private BGradeService bGradeService;
	/**
	 * 列表页面
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET})
	public String list(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BGrade entity = new BGrade();
		entity.getMap().put("pageSize", 15);
		entity.getMap().put("pageIndex", 0);
		entity.getMap().put("sort", 0);
		List<BUserInfo> platfromList = bGradeService.queryPageList(entity);
		int page_all = bGradeService.count(entity);
		int pageSum = page_all%15==0 ? page_all/15 :(page_all/15)+1;
		model.addAttribute("List", platfromList);
		model.addAttribute("pageSum", pageSum);//总页数
		model.addAttribute("totalSize", page_all);//总页数
		model.addAttribute("pageNo", 1);
		return "admin/gradeManager/list";
	}
	
	@RequestMapping(value = "newPage.html", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage(Model model, HttpServletRequest request){
		Map<String,Object> map = new HashMap<>();
		BGrade entity = new BGrade();
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		int pageStartRow = (pageNo - 1) * 15 ;// 每页的起始数
		entity.getMap().put("pageIndex", pageStartRow);
		entity.getMap().put("pageSize", 15);
		entity.getMap().put("sort", 0);
		List<BUserInfo> platfromList = bGradeService.queryPageList(entity);
		Integer count = bGradeService.count(entity);
		int pageSum = count%15==0 ? count/15 :(count/15)+1;
		map.put("platfromList", platfromList);
		map.put("count", count);
		map.put("pageSum", pageSum);
		map.put("pageNo", pageNo);
		return map;
    }
	
	@RequestMapping(value = "updateState.html", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult update(Model model, HttpServletRequest request,BGrade bGrade){
		bGrade.setState(GradeState.驳回.getValue());
		int flag=bGradeService.update(bGrade);
		if(flag>0){
		    return JsonResult.success("操作成功", flag);
		}else{
			return JsonResult.success("操作失败", flag);	
		}
    }
}
